package com.haoshuang.zhouzhoubang.ui.dialog

import android.content.Context

import android.widget.ImageView
import android.widget.TextView

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.model.bean.TimeBean8
import com.haoshuang.zhouzhoubang.ui.adapter.TimeCheckAdapter
import com.haoshuang.zhouzhoubang.ui.adapter.TimeCheckAdapter2

import com.lxj.xpopup.core.BottomPopupView
import com.mufeng.libs.utils.toast


class TimelDialog(
    context: Context,
    listTime: ArrayList<TimeBean8>,
    private val onSubmit: (service_day:String,service_time:String) -> Unit
) : BottomPopupView(context){
    override fun getImplLayoutId(): Int = R.layout.dialog_time
    var list2=listTime
    var list= arrayListOf<TimeBean8.Timer>()
    var service_day=""
    var service_time=""
    override fun initPopupContent() {
        super.initPopupContent()
        var ivDel=findViewById<ImageView>(R.id.ivDel)
        var tvCommit=findViewById<TextView>(R.id.tvCommit)
        var rvList2=findViewById<RecyclerView>(R.id.rvList2)
        var rvList=findViewById<RecyclerView>(R.id.rvList)
        var lay=LinearLayoutManager(context)
        lay.orientation=LinearLayoutManager.HORIZONTAL
        rvList.layoutManager=lay
        rvList2.layoutManager=GridLayoutManager(context,4)
        list2[0].isCheck=1
        service_day= list2[0].date_timestamp
        list.clear()
        list.addAll( list2[0].timer)
        var adapter= TimeCheckAdapter(list2)
        rvList.adapter=adapter
        var adapter2= TimeCheckAdapter2(list)
        adapter.setOnItemClickListener { adapter, view, position ->
            var bean=list2[position]
            if (bean.isCheck==0){
                for (b in list2){
                    b.isCheck=0
                }
                list2[position].isCheck=1
                service_day=list2[position].date_timestamp
                list.clear()
                list.addAll(  list2[position].timer)
                service_time=""
                adapter.notifyDataSetChanged()
                adapter2.notifyDataSetChanged()
            }

        }
        adapter2.setOnItemClickListener { adapter, view, position ->
            var bean=list[position]
            if (bean.enable){
                if (bean.isCheck==0){
                    for (b in list){
                        b.isCheck=0
                    }
                    list[position].isCheck=1
                    service_time= list[position].time
                    adapter2.notifyDataSetChanged()
                }
            }else{
                toast("当前时间不能选择")
            }
        }
        rvList2.adapter=adapter2
        ivDel.setOnClickListener {
            dismiss()
        }
        tvCommit.setOnClickListener {
            if (service_time.isNullOrBlank()){
                toast("时间段不能为空")
                return@setOnClickListener
            }
            dismiss()
            onSubmit(service_day,service_time)
        }
    }


}